package clei.sample

import android.arch.core.executor.ArchTaskExecutor
import android.arch.paging.AsyncPagedListDiffer
import android.arch.paging.PagedList
import android.arch.paging.PagedListAdapter
import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.support.v7.app.AppCompatActivity
import android.support.v7.recyclerview.extensions.ListAdapter
import android.support.v7.util.DiffUtil
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.google.gson.annotations.SerializedName
import java.util.concurrent.Executors

class MainActivity : AppCompatActivity() {

    private lateinit var tvLoading:TextView
    private lateinit var recyclerView: RecyclerView

    private lateinit var adapter: LanguageAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        tvLoading = findViewById(R.id.tvLoading)
        recyclerView = findViewById(R.id.recyclerView)

        recyclerView.layoutManager = LinearLayoutManager(this)
        adapter= LanguageAdapter(this)
        recyclerView.adapter= adapter


        val config = PagedList.Config.Builder()
                .setPageSize(15)
                .setPrefetchDistance(2)
                .setInitialLoadSizeHint(15)
                .setEnablePlaceholders(false)
                .build()

        val pageList = PagedList.Builder(LanguageDataSource(), config)
                .setNotifyExecutor {
                    Handler(Looper.getMainLooper()).post {it.run()}
                }
                .setFetchExecutor(Executors.newFixedThreadPool(2))
                .build()


        adapter.submitList(pageList)

    }
}


class LanguageAdapter(private val context: Context) : PagedListAdapter<Language, ViewHolder>(languageDiff) {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        Log.e(javaClass.simpleName, "onCreateVH")
        return ViewHolder(LayoutInflater.from(context)
                .inflate(R.layout.item_language, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        Log.e(javaClass.simpleName, "onBindVH")
        val language = getItem(position)
        holder.apply {
            language?.let {
                it.apply {
                    tvName.text = name
                    tvMainPage.text = mainPage
                    tvActivity.text = activity.toString()
                }
            }
        }
    }

}


val languageDiff = object: DiffUtil.ItemCallback<Language>() {
    override fun areItemsTheSame(oldItem: Language?, newItem: Language?): Boolean {
        if (oldItem == null) {
            return false
        }

        if (newItem == null) {
            return false
        }

        return oldItem.name == newItem.name
        && oldItem.mainPage == newItem.mainPage
        && oldItem.activity == newItem.activity
    }

    override fun areContentsTheSame(oldItem: Language?, newItem: Language?): Boolean {
        if (oldItem == null) {
            return false
        }

        if (newItem == null) {
            return false
        }

        return oldItem.name == newItem.name
                && oldItem.mainPage == newItem.mainPage
                && oldItem.activity == newItem.activity
    }

}


class ViewHolder(itemView:View) : RecyclerView.ViewHolder(itemView) {
    val tvName = itemView.findViewById<TextView>(R.id.tvName)
    val tvMainPage = itemView.findViewById<TextView>(R.id.tvMainPage)
    val tvActivity = itemView.findViewById<TextView>(R.id.tvActivity)
}


data  class  Language(
        var name:String,
        @SerializedName("mainpage") var mainPage:String,
        var activity:Int
)


data class MyResponse(
        @SerializedName("Code") var code:Int,
        @SerializedName("Msg") var message:String,
        @SerializedName("Data") var data:ArrayList<Language>
)